actions = function() {
	//定义搜索权限窗体
	var ACTION_SEARCH_WIN = null;
	//定义添加修改权限窗体
	var ACTION_ADDOREDIT_WIN = null;
	return {
		/*--------------------------------------窗体定义--------------------------------------------*/
		//定义权限GRID窗体
		panel: function() {
			var store = new Store({
				fields: ['id', 'code', 'url', 'remark'],
				proxy: {
			        type: 'ajax',
			        url: '../admin/action.do?index',
			        reader: {
			            type: 'json',
			            root: 'datas',
			            totalProperty: 'total'
			        }
			    }
			});
			
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				code: '',
				url: ''
			};
			store.load();
			
			var pageToolBar = new PagingToolbar({
	            store: store,
	            draggable : false,
	            displayInfo: true
	        });
			
			var grid = new Grid({
				title: '权限中心',
				id: 'actiongrid',
				region: 'center',
				store: store,
				minColumnWidth: 35,
				stripeRows: true,
				bbar : pageToolBar,
				tbar: [{
					text: '刷新',
					scale: 'medium',
					iconCls: 'icon-refresh',
					handler: function() {
						store.load();
					}
				}, "-", {
					text: '搜索',
					scale: 'medium',
					iconCls: 'icon-search',
					handler: function() {
						actions.searchAction();
					}
				}, "-", {
					id: 'actionAdd',
					text: '添加',
					scale: 'medium',
					iconCls: 'icon-add',
					disabled: true,
					handler: function() {
						actions.addAction();
					}
				}, "-", {
					id: 'actionEdit',
					text: '修改',
					scale: 'medium',
					iconCls: 'icon-edit',
					disabled: true,
					handler: function() {
						actions.editAction();
					}
				}, "-", {
					id: 'actionRemove',
					text: '删除',
					scale: 'medium',
					iconCls: 'icon-remove',
					disabled: true,
					handler: function() {
						actions.removeAction();
					}
				}],
				columns: [
		             new Ext.grid.RowNumberer(),
		             {header: '序号', dataIndex: 'id', hidden: true},
		             {header: '权限代码', dataIndex: 'code', width: 300},
		             {header: '访问路径', dataIndex: 'url', width: 400},
		             {header: '备注说明', dataIndex: 'remark', flex: 1}
				],
				listeners: {
					select: function(fn, rec, index, eOpts) {
					}
				}
			});
			
			var panel = new Panel({
				border: false,
				layout: 'border',
				items: [grid]
			});
			
			return panel;
		},
		//定义搜索窗体
		searchWin: function() {
			if (ACTION_SEARCH_WIN == null) {
				ACTION_SEARCH_WIN = new Window({
					id: 'actionSearchWin',
				    title: '搜索权限',
				    height: 200,
				    width: 400,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	frame: false,
						autoHeight: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'code',
				        	fieldLabel: '权限代码'
				        }, {
				        	name: 'url',
				        	fieldLabel: '访问路径'
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		actions.searchSureAction(fom);
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		ACTION_SEARCH_WIN.hide();
				        	}
				        }]
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        	}
			        }
				});
			}
			ACTION_SEARCH_WIN.show();
		},
		//定义添加修改窗体
		addOrEditWin: function() {
			if (ACTION_ADDOREDIT_WIN == null) {
				ACTION_ADDOREDIT_WIN = new Window({
					id: 'actionAddOrEditWin',
				    title: '编辑权限信息',
				    height: 230,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	id: 'actionAddOrEditForm',
				    	frame: false,
						autoHeight: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'id',
				        	hidden: true
				        }, {
				        	name: 'code',
				        	fieldLabel: '权限代码',
				        	allowBlank: false
				        }, {
				        	name: 'url',
				        	fieldLabel: '访问路径',
				        	allowBlank: false
				        }, {
				        	xtype: 'textareafield',
				        	name: 'remark',
				        	fieldLabel: '备注说明'
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		if (fom.isValid()) {
				        			actions.sumbitAction(fom);
				        		}
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		ACTION_ADDOREDIT_WIN.hide();
				        	}
				        }]
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        	}
			        }
				});
			}
			ACTION_ADDOREDIT_WIN.show();
		},
		
		/*--------------------------------------事件定义--------------------------------------------*/
		//搜索窗体事件
		searchAction: function() {
			actions.searchWin();
		},
		
		//搜索确定事件
		searchSureAction: function(fom) {
			var store = Ext.getCmp('actiongrid').getStore();
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				code: fom.findField('code').value,
				url: fom.findField('url').value
			};
			store.load();
			ACTION_SEARCH_WIN.hide();
		},
		
		//添加窗体事件
		addAction: function() {
			actions.addOrEditWin();
		},
		
		//修改窗体事件
		editAction: function() {
			var sel = Ext.getCmp('actiongrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要修改的权限！');
			} else {
				actions.addOrEditWin();
				Ext.getCmp('actionAddOrEditForm').getForm().loadRecord(sel[0]);
			}
		},
		
		//提交确认事件
		sumbitAction: function(fom) {
			var url = '../admin/action.do?edit';
			var id = fom.findField('id').value;
			if (id == '') {
				url = '../admin/action.do?add';
			}
			FormSubmit(fom, url, {}, function(obj) {
				Ext.getCmp('actiongrid').getStore().load();
				ACTION_ADDOREDIT_WIN.hide();
			});
		},
		
		//删除权限事件
		removeAction: function() {
			var sel = Ext.getCmp('actiongrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要删除的权限！');
			} else {
				Ext.MessageBox.confirm('确认', '您确认要删除该权限吗？', function(btn) {
					if (btn == "yes") {
						var res = RequestByUrl('../admin/action.do?remove', {
							id: sel[0].get('id')
						});
						Ext.getCmp('actiongrid').getStore().load();
						Ext.Show.msg(res.title, '');
					}
				});
			}
		}
	};
}();